﻿using System;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;
using Baosight.Platform.Core.UI;
using Microsoft.Practices.Unity;
using Baosight.ZNH.ZMCS.UI.Utils;


namespace Baosight.ZNH.ZMCS.UI
{
    /// <summary>
    /// 变量  明细页面  
    /// </summary>
    /// Title:ZMCS46
    /// 对应ServiceZMCS46.java
    /// Copyright:Baosight Software LTD.co Copyright (c) 2011
    /// Company:上海宝信软件股份有限公司
    /// Author: 张志芳
    /// Version: 1.0
    /// History:
    ///       2010-12-14  张志芳   [创建]
    ///
    public partial class ZMCS46 : EFViewCmes
    {
        private MineModelBlock _block = new MineModelBlock();//构造前台下拉列表

        public ZMCS46()
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
        }
        /// <summary>
        /// Load事件中初始化父类EFViewCmesProperty的值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.InsertPanel.GetChildObjects<EFDataField>(null, true);
            _view.ListPageNo = "ZMCS46";
            _view.ViewType = "tree";

            _view.Args.Add("initArgs", this.fdKeyId.GetDataValue().ToString());
            _view.Args.Add("fdParentId", this.fdKeyId.GetDataValue().ToString());
            _view.Args.Add("fdParentTable", this.fdParentTable.GetDataValue().ToString());
            _view.Args.Add("isLeaf", this.isLeaf.GetDataValue().ToString());

            base.ViewProperty = _view;
        }

        /// <summary>
        /// 重新初始化方法
        /// </summary>
        /// <param name="initData"></param>
        public override void Initialize(EIInfo initData)
        {
            initData.AddBlock(_block.GetUseingStatus(false));//状态 下拉
            initData.AddBlock(_block.GetVariableCycle());//周期 下拉
            initData.AddBlock(_block.IsTrueOrFalse(false));//是否 下拉
            initData.AddBlock(_block.GetVariableType(false));//是否 下拉
            base.Initialize(initData);
            //判断页面时新增页还是编辑页
            InitDataSet(initData);
        }

        /// <summary>
        /// 给Mode付值
        /// </summary>
        /// <param name="initData"></param>
        private void InitDataSet(EIInfo initData)
        {
            base.Mode = (OperationMode)Enum.Parse(typeof(OperationMode), "Update", true);
        }

        /// <summary>
        /// 点击确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        public override void OnUpdateButtonClick(object sender, RoutedEventArgs e)
        {
            base.OnSureButtonClick(sender, e);
        }

        #region 公用方法

        /// <summary>
        /// 重写基类方法：根据数据的改变更新主页面的数据
        /// </summary>
        /// <param name="data"></param>
        public override void UpdateHomePageData(EIInfo data)
        {
            // 数据发布
            IEventAggregator eventAggreator = Container.Resolve<IEventAggregator>();
            EIDataPublishedEvent dataPublishedEvent = eventAggreator.GetEvent<EIDataPublishedEvent>();
            dataPublishedEvent.Publish(data);
        }

        /// <summary>
        /// 校验前的工作
        /// </summary>
        /// <returns></returns>
        public override bool ThisValidateField()
        {
            string _value = this.fdInterfaceType.Value == null ? "" : this.fdInterfaceType.Value.ToString();
            if (_value.Equals("0"))
            {
                this.fdTagNo.eRegex = "^[A-Za-z0-9\\._@]{1,100}$";
                this.fdTagNo.eValidateErrorPrompt = "输入1-100个字符，包括数字、字母、下划线、点号、@符";
                this.fdTableName.eRegex = null;
                this.fdColumn.eRegex = null;
            }
            else
            {
                this.fdTagNo.eRegex = null;
            }
            return true;
        }

        /// <summary>
        /// 数据源发生改变时
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdDatainterfaceId_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null)
            {
                return;
            }

            EFDataField field = sender as EFDataField;
            EFComboBox box = field.Content as EFComboBox;
            if (box.SelectedItem == null)
            {
                return;
            }
            string DataType = box.SelectedItem.GetType().GetProperty("type").GetValue(box.SelectedItem, null) as string;
            this.fdInterfaceType.Value = DataType;
        }

        /// <summary>
        /// 根据数据源类型的不同显示不同的输入框
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdInterfaceType_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null || e.NewValue.ToString().Equals(""))
            {
                return;
            }

            string DataType = e.NewValue.ToString();
            if (DataType.Equals("0"))
            {
                this.fdTagNo.Visibility = Visibility.Visible;
                this.fdTableName.Visibility = Visibility.Collapsed;
                this.fdColumn.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.fdTagNo.Visibility = Visibility.Collapsed;
                this.fdTableName.Visibility = Visibility.Visible;
                this.fdColumn.Visibility = Visibility.Visible;
            }
        }

        /// <summary>
        /// 数据源
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdInterfaceType_Loaded(object sender, RoutedEventArgs e)
        {
            var dataType = this.fdInterfaceType.Value;
            //iHistory数据源
            if (null != dataType && dataType.ToString() == "0")
            {
                this.fdTagNo.Visibility = Visibility.Visible;
                this.fdTableName.Visibility = Visibility.Collapsed;
                this.fdColumn.Visibility = Visibility.Collapsed;
            }
            else
            {
                this.fdTagNo.Visibility = Visibility.Collapsed;
                this.fdTableName.Visibility = Visibility.Visible;
                this.fdColumn.Visibility = Visibility.Visible;
            }
        }
        #endregion
    }
}
